// accounting/subject_detail/subject_detail.js
let listBehavior = require('../../behavior/list')
Page({
  behaviors: [ listBehavior, ],

  /**
   * 页面的初始数据
   */
  data: {
    list: [],

    quickFlag: false,

    searchList:[
      {field_id: ['account_period_s','account_period_e'],field_name: '会计期',type: 'reference1', optionList: [],optionId: 'account_period',optionName: 'account_period'},
      {field_id: 'account_id',field_name: '科目选择',type: 'reference1', optionList: [],optionId: 'account_id',optionName: 'account_name'},
      {field_id: ['account_level_s','account_level_e'],field_name: '科目级别',type: 'num'},
    ],
    searchObj: {
      account_id: '',
      sortby: "no",
      account_period_s: '',
      account_period_e: '',
    },
    account_name: '', //显示
    accountList: [],  //科目列表

  },
  //显示搜索
  showQuick(){
    this.setData({
      quickFlag: !this.data.quickFlag,
    })
  },
  onRadioChange(e){
    this.setData({[`searchObj.sortby`]:  e.detail});
  },
  navTo(e){
    let {url} = e.currentTarget.dataset;
    wx.navigateTo({
      url
    })
  },
  searchChange({detail}){
    if (detail.field_id.includes("account_period_s")) {
      let s = detail.val[0],
        e = detail.val[1];
      this.getOptionsList(['account_id',3,'Account/query_voucher_account_used',{account_period_s: s, account_period_e: e}]).then(res=>{
        let {account_id} = res,
          first = account_id[0] && account_id[0].account_id,
          obj = {
            [`searchList[0].dfltVal`]: '',
            [`searchList[1].dfltVal`]: '',
            [`searchList[1].optionList`]: account_id,
            [`accountList`]: account_id
          };
        if (first){
          obj[`searchObj.account_id`] = first;
          obj[`searchList[1].dfltVal`] = first;
          obj[`account_name`] = account_id[0].account_name;
        }
        this.setData(obj);
      })
    }
  },
  searchData({detail}){
    let {searchObj,accountList} = this.data,
      obj = {};
    wx.$each(detail,(i,item)=>{
      let val = item.CompareValue,
        fieldArr = item.CompareWith.split(',');
      wx.$each(fieldArr,(ind,ite)=>{
        let value = val[ind];
        if (['account_level_s','account_level_e'].includes(ite) && !value) value = 0;
        searchObj[ite] = value;
      });
      //科目
      if (item.CompareWith.indexOf('account_id')>=0){
        let account = accountList.find(item=>{
          return item.account_id == val;
        });
        if (account){
          searchObj.account_id = account.account_id;
          obj.account_name = account.account_name;
        }
      }
    });
    obj = {...obj,searchObj};
    this.setData(obj);
    this.getFirstPage();
  },
  getDataList(){
    let {searchObj} = this.data;
    wx.$post(3,'Voucher/query_all_voucher_statistics_lm',searchObj).then(res=>{
      if (res.errcode != 0)return;
      let list= res.data.row || [];
      this.setData({list,limit: list.length,total: list.length})
    })
  },


  //获取下拉数据
  getOptionsList (arg){
    let [key,v,url,post] = arg;
    return new Promise((resolve,reject) => {
      wx.$post(v, url, post).then(res=>{
        if (res.errcode != 0){
          reject();
          return;
        }
        let data = res.data.row || [],
          list = data;
        resolve({[key]: list});
      });
    });
  },
  ready(options){
    let {account_period_s,account_period_e,include_id} = options,
      obj = {};
    let pm = this.getOptionsList( ['account_period',3,'Account/query_all_account_period',{"limit":0,"page":1}]),
      pm2 = pm.then(res=>{
        let {account_period} = res,
          start = account_period[0] && account_period[0].account_period,
          end = start;
        account_period_s && (start = account_period_s);
        account_period_e && (end = account_period_e);
        obj[`searchList[0].optionList`] = account_period;
        obj[`searchList[0].dfltVal`] = [start,end];
        obj[`searchObj.account_period_s`] = start;
        obj[`searchObj.account_period_e`] = end;
        return this.getOptionsList(['account_id',3,'Account/query_voucher_account_used',{account_period_s: start, account_period_e: end}])
      });
    pm2.then(res=>{
      let {account_id} = res,
        first = account_id[0] && account_id[0].account_id,
        name = account_id[0] && account_id[0].account_name
      obj[`searchList[1].optionList`] = account_id;
      obj[`accountList`] = account_id;
      if (include_id){
        first = include_id;
        let include = account_id.find(item=>{
          return item.account_id == include_id;
        });
        name = include && include.account_name;
      }
      if (first){
        obj[`searchObj.account_id`] = first;
        obj[`searchList[1].dfltVal`] = first;
        obj[`account_name`] = name;
      }

      this.setData(obj);
      this.getDataList();
    },err=>{
      this.setData(obj);
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.$setBarTitle();
    wx.$getAuth('all_voucher_statistics').then(res=>{
      if (!res.tableSee) {
        wx.$toast('抱歉，您没有权限查看该模块数据！',1000).then(()=>{
          wx.navigateBack({ delta:1 });
        });
        return
      }

      this.ready(options);
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})
